package com.yu.subject.domain.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yu.subject.common.enums.CategoryTypeEnum;
import com.yu.subject.common.exception.CodeAndMessage;
import com.yu.subject.common.exception.JavaSmException;
import com.yu.subject.common.util.BeanCopyUtil;
import com.yu.subject.domain.entity.SubjectCategoryBO;
import com.yu.subject.domain.service.SubjectCategoryDomainService;
import com.yu.subject.infra.basic.entity.SubjectCategory;
import com.yu.subject.infra.basic.service.SubjectCategoryService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @author 路亚
 * @version 0.1
 * @className SubjectCategoryDomainServiceImpl
 * @description
 * @date 2024/12/21 20:21
 * @since jdk11
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class SubjectCategoryDomainServiceImpl implements SubjectCategoryDomainService {

    private final SubjectCategoryService subjectCategoryService;

    @Override
    public void add(SubjectCategoryBO subjectCategoryBO) {
        if (log.isInfoEnabled()){
            log.info("新增题目分类，参数：{}", subjectCategoryBO);
        }
        SubjectCategory subjectCategory = new SubjectCategory();
        BeanUtils.copyProperties(subjectCategoryBO, subjectCategory);
        subjectCategoryService.save(subjectCategory);
    }

    @Override
    public List<SubjectCategoryBO> queryPrimaryCategory() {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("category_type", CategoryTypeEnum.PRIMARY.getCode());
        List<SubjectCategory> list = subjectCategoryService.list(queryWrapper);
        List<SubjectCategoryBO> result = BeanCopyUtil.copyListProperties(list, SubjectCategoryBO.class);
        return result;
    }

    @Override
    public List<SubjectCategoryBO> queryCategoryByPrimary(SubjectCategoryBO subjectCategoryBO) {
        QueryWrapper queryWrapper = new QueryWrapper();
        if (subjectCategoryBO.getParentId() != null){
            queryWrapper.eq("parent_id", subjectCategoryBO.getParentId());
        }
        if (subjectCategoryBO.getCategoryType() != null){
            queryWrapper.eq("category_type", subjectCategoryBO.getCategoryType());
        }
        if (!StringUtils.isEmpty(subjectCategoryBO.getCategoryName())){
            queryWrapper.like("category_name", subjectCategoryBO.getCategoryName());
        }
        if (!StringUtils.isEmpty(subjectCategoryBO.getImageUrl())){
            queryWrapper.eq("image_url", subjectCategoryBO.getImageUrl());
        }
        queryWrapper.eq("is_deleted", 0);
        List list = subjectCategoryService.list(queryWrapper);
        List result = BeanCopyUtil.copyListProperties(list, SubjectCategoryBO.class);
        return result;
    }

    @Override
    public void updateById(SubjectCategoryBO subjectCategoryBO) {
        SubjectCategory subjectCategory = new SubjectCategory();
        BeanUtils.copyProperties(subjectCategoryBO, subjectCategory);
        if (subjectCategory.getId() == null){
            throw new JavaSmException(CodeAndMessage.CATEGORY_ID_NOT_NULL);
        }
        subjectCategoryService.updateById(subjectCategory);
    }

    @Override
    public void deleteById(Long id) {
        subjectCategoryService.deleteById(id);
    }
}
